package top.lc951.myandroid.views.card;

import android.os.Build;
import android.support.annotation.RequiresApi;
import android.support.v4.view.ViewPager;
import android.view.View;

public class CardOverlayTransformerCenter extends OverlayTransformerLan{
    public CardOverlayTransformerCenter(int overlayCount, float minScale) {
        super(overlayCount, minScale);
    }
    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    public void transformPage(View page, float position) {
        int pageWidth = page.getWidth();
        int pageHeight = page.getHeight();
        if (mViewPager == null) {
            mViewPager = (ViewPager) page.getParent();
        }
        int current = mViewPager.getCurrentItem();
        if (position <= 0) {
            page.setTranslationX(-position * page.getWidth()
                    +  0.85f*mOffset * position
                    + 1.6f * current * mOffset
            );
            float scaleFactor = (1 + position) * (1 - mMinScale) + mMinScale;
            page.setScaleX(scaleFactor);
            page.setScaleY(scaleFactor);

            page.setTranslationZ(position);
            page.setTranslationY((-position) * mOffset*0.5f);

            page.setPivotX(-pageWidth/3);
        } else if (position <= overlayCount - 1) {
            //移动X轴坐标，使得卡片在同一坐标
            page.setTranslationX(-position * page.getWidth()
                    +   mOffset * position
                    + 1.6f * current * mOffset
            );
            float scaleFactor = (1 - position) * (1 - mMinScale) + mMinScale;
            page.setScaleX(scaleFactor);
            page.setScaleY(scaleFactor);

            page.setTranslationY((position) * mOffset*0.5f);
            page.setTranslationZ(-position);
            page.setPivotX(pageWidth);
        }else{
            page.setAlpha(0f);
        }
    }
}
